/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.hardcodeando.ui;

import ar.com.hardcodeando.algorithm.NLFSR;
import ar.com.hardcodeando.algorithm.NLFSRFunction;
import ar.com.hardcodeando.dto.NLFSRDTO;
import ar.com.hardcodeando.ui.utils.AlgorithmStateStorage;
import ar.com.hardcodeando.ui.utils.ComboItem;
import ar.com.hardcodeando.ui.utils.SaveFile;
import java.awt.Color;
import static java.awt.image.ImageObserver.WIDTH;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author connie
 */
public class NLFSREvaluar extends javax.swing.JPanel {
    
    
    private int seed;
    private int length;
    private String seedStr;
    private NLFSRFunction function;
    private NLFSR nlfsr;
    private int s0;
    private String key;
    private ArrayList<JRadioButton> wronganswers;
    private ArrayList<JRadioButton> rightanswers;
    private int it = 0;
    private int maxIt;

    /**
     * Creates new form NLFSREvaluar
     */
    public NLFSREvaluar() {
        initComponents();
        
        jTabbedPane1.setEnabledAt(1, false);
        jTabbedPane1.setEnabledAt(2, false);
        jTabbedPane1.setEnabledAt(3, false);
        jTabbedPane1.setEnabledAt(4, false);
        jTabbedPane1.setEnabledAt(5, false);
        
        wronganswers = new ArrayList<>();
        wronganswers.add(jRadioButton2);
        wronganswers.add(jRadioButton3);
        wronganswers.add(jRadioButton4);
        wronganswers.add(jRadioButton5);
        wronganswers.add(jRadioButton7);
        wronganswers.add(jRadioButton8);
        wronganswers.add(jRadioButton11);
        wronganswers.add(jRadioButton12);
        
        rightanswers = new ArrayList<>();
        rightanswers.add(jRadioButton1);
        rightanswers.add(jRadioButton6);
        rightanswers.add(jRadioButton9);
        rightanswers.add(jRadioButton10);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel6 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel18 = new javax.swing.JLabel();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jLabel19 = new javax.swing.JLabel();
        jRadioButton7 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();
        jRadioButton9 = new javax.swing.JRadioButton();
        jLabel20 = new javax.swing.JLabel();
        jRadioButton10 = new javax.swing.JRadioButton();
        jRadioButton11 = new javax.swing.JRadioButton();
        jRadioButton12 = new javax.swing.JRadioButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton18 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton19 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton20 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton21 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton10 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jButton22 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(850, 840));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("NLFSR - Modo Evaluar");
        jLabel1.setAlignmentX(0.5F);

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel16.setText("Introducción");

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel17.setText("¿Qué tipos de números se generan con NLFSR?");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Aleatorios");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Pseudo-aleatorios");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("No generan números");

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel18.setText("¿Qué tipo de función se puede utilizar?");

        buttonGroup2.add(jRadioButton4);
        jRadioButton4.setText("Cualquier función lógica");

        buttonGroup2.add(jRadioButton5);
        jRadioButton5.setText("Cualquier funcion logica aplicable a la misma cantidad de bits del registro");

        buttonGroup2.add(jRadioButton6);
        jRadioButton6.setText("Cualquier funcion logica aplicable al registro");
        jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton6ActionPerformed(evt);
            }
        });

        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel19.setText("¿Que bit del registro se agrega a la clave?");

        buttonGroup3.add(jRadioButton7);
        jRadioButton7.setText("Ninguno");

        buttonGroup3.add(jRadioButton8);
        jRadioButton8.setText("r0");

        buttonGroup3.add(jRadioButton9);
        jRadioButton9.setText("rn-1");

        jLabel20.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel20.setText("¿En qué posición del registro se ubica el x calculado a traves de la función?");

        buttonGroup4.add(jRadioButton10);
        jRadioButton10.setText("En r0 luego de desplazar el registro a la derecha");

        buttonGroup4.add(jRadioButton11);
        jRadioButton11.setText("En rn-1 luego de desplazar el registro a la izquierda");

        buttonGroup4.add(jRadioButton12);
        jRadioButton12.setText("Se agrega en rn");

        jButton13.setText("Corregir");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton14.setText("Mostrar");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton15.setText("Siguiente");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton12)
                    .addComponent(jRadioButton11)
                    .addComponent(jRadioButton10)
                    .addComponent(jRadioButton9)
                    .addComponent(jRadioButton8)
                    .addComponent(jRadioButton7)
                    .addComponent(jRadioButton6)
                    .addComponent(jRadioButton5)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton1)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17)
                    .addComponent(jLabel18)
                    .addComponent(jRadioButton4)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20))
                .addContainerGap(437, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton14)
                .addGap(9, 9, 9)
                .addComponent(jButton15)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel16)
                .addGap(18, 18, 18)
                .addComponent(jLabel17)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addGap(18, 18, 18)
                .addComponent(jLabel18)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton6)
                .addGap(18, 18, 18)
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton9)
                .addGap(18, 18, 18)
                .addComponent(jLabel20)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton12)
                .addGap(45, 45, 45)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton13)
                    .addComponent(jButton14)
                    .addComponent(jButton15))
                .addContainerGap(160, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Introduccion", jPanel6);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Paso 1: Definir semilla");

        jLabel5.setText("Ingrese un número decimal a usar como semilla y expréselo en binario. Puede ingresar números que se expresen en hasta 4 bits.");

        jLabel2.setText("semilla:");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel21.setText("r:");

        jButton1.setEnabled(false);
        jButton1.setLabel("Siguiente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Validar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton18.setLabel("Guardar");
        jButton18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton18ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(357, 357, 357)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel21)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))))
                .addContainerGap(238, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton18)
                .addGap(58, 58, 58)
                .addComponent(jButton2)
                .addGap(55, 55, 55)
                .addComponent(jButton1)
                .addGap(69, 69, 69))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton18))
                .addGap(264, 264, 264))
        );

        jButton18.getAccessibleContext().setAccessibleName("Guardar");

        jTabbedPane1.addTab("Paso 1", jPanel1);

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel22.setText("Paso 2: Definir función");

        jLabel8.setText("Seleccione una función acorde a la semilla:");

        jLabel9.setText("<html>f(r<sub>0</sub>,r<sub>1</sub>,r<sub>2</sub>,r<sub>3</sub>) = </html>");

        jButton3.setText("Siguiente");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Validar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton19.setLabel("Guardar");
        jButton19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton19ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel22)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton19)
                        .addGap(42, 42, 42)))
                .addComponent(jButton4)
                .addGap(38, 38, 38)
                .addComponent(jButton3)
                .addGap(61, 61, 61))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(348, 348, 348)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(336, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel22)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 353, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton19))
                .addGap(180, 180, 180))
        );

        jTabbedPane1.addTab("Paso 2", jPanel2);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel12.setText("Paso 3: Definir el primer bit clave y calcular x");

        jLabel13.setText("Indicar el primer bit clave y calcular x usando la función elegida anteriormente:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "registro", "bit clave", "x"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton6.setEnabled(false);
        jButton6.setLabel("Siguiente");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Validar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton11.setText("Mostrar");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton20.setLabel("Guardar");
        jButton20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton20ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(403, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton20)
                .addGap(32, 32, 32)
                .addComponent(jButton11)
                .addGap(38, 38, 38)
                .addComponent(jButton7)
                .addGap(18, 18, 18)
                .addComponent(jButton6)
                .addGap(61, 61, 61))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(234, 234, 234)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton7)
                    .addComponent(jButton11)
                    .addComponent(jButton20))
                .addContainerGap(380, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Paso 3", jPanel3);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel11.setText("Paso 4: Obtener el nuevo registro");

        jLabel14.setText("Indicar el nuevo registro obtenido:");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "registro", "clave", "x", "nuevo registro"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jButton8.setText("Siguiente");
        jButton8.setEnabled(false);
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Validar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton5.setText("Mostrar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton21.setLabel("Guardar");
        jButton21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton21ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel14)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton21)
                        .addGap(42, 42, 42)))
                .addComponent(jButton5)
                .addGap(26, 26, 26)
                .addComponent(jButton9)
                .addGap(46, 46, 46)
                .addComponent(jButton8)
                .addGap(55, 55, 55))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel14)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(220, 220, 220)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton8)
                    .addComponent(jButton9)
                    .addComponent(jButton5)
                    .addComponent(jButton21))
                .addContainerGap(378, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Paso 4", jPanel4);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel10.setText("Paso 5: Definir clave");

        jLabel15.setText("Usando el nuevo registro, repita los pasos 3 y 4 hasta obtener una clave definida. Una vez finalizadas las iteraciones indique la clave obtenida, su período y su valor en decimal:");

        jTextField3.setEnabled(false);
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jTextField4.setEnabled(false);

        jTextField5.setEnabled(false);

        jLabel4.setText("Clave:");

        jLabel6.setText("Periodo:");

        jLabel7.setText("Número:");

        jButton10.setText("Validar");
        jButton10.setEnabled(false);
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton12.setText("Mostrar");
        jButton12.setEnabled(false);
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "registro", "clave", "x", "nuevo registro"
            }
        ));
        jScrollPane3.setViewportView(jTable3);

        jButton16.setText("Validar iteración");
        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton16ActionPerformed(evt);
            }
        });

        jButton17.setText("Mostrar iteracion");
        jButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton17ActionPerformed(evt);
            }
        });

        jButton22.setLabel("Guardar");
        jButton22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton22ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel15))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton22)
                                .addGap(42, 42, 42)
                                .addComponent(jButton12)
                                .addGap(29, 29, 29)
                                .addComponent(jButton10)
                                .addContainerGap())
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(48, 48, 48)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel4))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField3)
                                            .addComponent(jTextField4)
                                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(38, 38, 38)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton17)
                                            .addComponent(jButton16))))
                                .addGap(188, 188, 188))))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel15)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jButton16)
                        .addGap(18, 18, 18)
                        .addComponent(jButton17)
                        .addGap(138, 138, 138)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(86, 86, 86)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton10)
                            .addComponent(jButton12)
                            .addComponent(jButton22))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 254, Short.MAX_VALUE))))
        );

        jTabbedPane1.addTab("Paso 5", jPanel5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(392, 392, 392)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //validate step 1
        try{
            seed = Integer.parseInt(jTextField1.getText());
            if(!Integer.toBinaryString(seed).equals(jTextField2.getText())){
                showPopup(jPanel1, "Registro de bits inválido.");
            }else{
                showPopup(jPanel1, "Semilla definida correctamente. Puede continuar con el próximo paso.");
                jButton1.setEnabled(true);
                length = Integer.toBinaryString(seed).length();
                seedStr = Integer.toBinaryString(seed);
            }
        }
        catch(NumberFormatException e){
            showPopup(jPanel1, "Semilla inválida.");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void enableStep1(){
        try{
            seed = Integer.parseInt(jTextField1.getText());
            jButton1.setEnabled(true);
            length = Integer.toBinaryString(seed).length();
            seedStr = Integer.toBinaryString(seed);
        }
        catch(NumberFormatException e){
            
        }
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jTabbedPane1.setEnabledAt(2, true);
        jTabbedPane1.setSelectedIndex(2);
        setUpStep2();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void setUpStep2(){
        jLabel9.setText(NLFSRCommon.getFunctionDescription(length));
        
        Map<Integer,NLFSRFunction> functions = NLFSRFunction.getFunctions();
        jComboBox1.removeAllItems();
        for (int i = 0; i < functions.size(); i++) {
            jComboBox1.addItem(new ComboItem(Integer.toString(i),functions.get(i).getDescription()));
        }
        jComboBox1.setSelectedIndex(0);
    }
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
        jTabbedPane1.setEnabledAt(3, true);
        jTabbedPane1.setSelectedIndex(3);
        setUpStep3();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void setUpStep3(){
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object []{ Integer.toBinaryString(seed), "", "" });
        
        maxIt = (int) Math.pow(2, length) - 1;
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        //validate step 2
        ComboItem item = (ComboItem) jComboBox1.getSelectedItem();
        Map<Integer,NLFSRFunction> functions = NLFSRFunction.getFunctions();
        function = functions.get(Integer.parseInt(item.getKey()));
        if(length < function.getMin())
            showPopup(jPanel2, "Funcion inválida.");
        else{
            showPopup(jPanel2, "Función definida correctamente. Puede continuar con el próximo paso.");
            jButton3.setEnabled(true);
            nlfsr = new NLFSR(function, seedStr);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void enableStep2(){
        ComboItem item = (ComboItem) jComboBox1.getSelectedItem();
        Map<Integer,NLFSRFunction> functions = NLFSRFunction.getFunctions();
        function = functions.get(Integer.parseInt(item.getKey()));
        jButton3.setEnabled(true);
        nlfsr = new NLFSR(function, seedStr);
    }
    
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        //validate step 3
        try{
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            String k = (String)model.getValueAt(0, 1);
            int x = Integer.parseInt(model.getValueAt(0, 2).toString());
            
            String message = new String();
            if(String.valueOf(NLFSRCommon.getKey(seedStr)) == null ? k != null : !String.valueOf(NLFSRCommon.getKey(seedStr)).equals(k)){
                message = message + "El bit clave es incorrecto. ";
            }
            if(x != function.getResult(nlfsr.getSeedArray(seedStr))){
                message = message + "El x calculado es incorrecto.";
            }
            
            if(message.isEmpty()){
                showPopup(jPanel3, "Bit clave y x correctos. Puede continuar con el proximo paso.");
                jButton6.setEnabled(true);
                s0 = x;
                key = k;
            }
            else{
                showPopup(jPanel3, message);
            }
            
        }catch(Exception e){
            showPopup(jPanel3, "El x calculado es incorrecto.");
        }
        
    }//GEN-LAST:event_jButton7ActionPerformed

    private void enableStep3(){
        try{
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            String k = (String)model.getValueAt(0, 1);
            int x = Integer.parseInt(model.getValueAt(0, 2).toString());            
            jButton6.setEnabled(true);
            s0 = x;
            key = k;
            
        }catch(Exception e){
            
        }
    }
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        
        //display step 4
        jTabbedPane1.setEnabledAt(4, true);
        jTabbedPane1.setSelectedIndex(4);
        
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
        
        model2.addRow(new Object[]{ seedStr, NLFSRCommon.getKey(seedStr), function.getResult(nlfsr.getSeedArray(seedStr)), "" });
    }//GEN-LAST:event_jButton6ActionPerformed

    private void setUpStep4(){
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
        
        model2.addRow(new Object[]{ seedStr, NLFSRCommon.getKey(seedStr), function.getResult(nlfsr.getSeedArray(seedStr)), "" });
    }
    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        //validate step 4
        DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
        String newSeed = model.getValueAt(0, 3).toString();
        String newSeedOk = NLFSRCommon.getSeedString(nlfsr.shiftSeed(nlfsr.getSeedArray(seedStr), s0));
        if(newSeed.equals(newSeedOk)){
            showPopup(jPanel4, "El nuevo registro es correcto. Puede continuar con el proximo paso.");
            jButton8.setEnabled(true);
        }
        else
            showPopup(jPanel4, "El nuevo registro es incorrecto. Intentelo nuevamente.");
        
    }//GEN-LAST:event_jButton9ActionPerformed

    private void enableStep4(){
        jButton8.setEnabled(true);        
    }
    
    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        //show step 5
        jTabbedPane1.setEnabledAt(5, true);
        jTabbedPane1.setSelectedIndex(5);
        
        DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
        
        String newSeedOk = NLFSRCommon.getSeedString(nlfsr.shiftSeed(nlfsr.getSeedArray(seedStr), s0));
        model.addRow(new Object[]{seedStr, NLFSRCommon.getKey(seedStr), function.getResult(nlfsr.getSeedArray(seedStr)), newSeedOk});
        model.addRow(new Object[]{"","","",""});
        
        nlfsr.Execute();
    }//GEN-LAST:event_jButton8ActionPerformed

    private void setUpStep5(){
        
        jTabbedPane1.setEnabledAt(5, true);
        jTabbedPane1.setSelectedIndex(5);
        
        DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
        
        String newSeedOk = NLFSRCommon.getSeedString(nlfsr.shiftSeed(nlfsr.getSeedArray(seedStr), s0));
        model.addRow(new Object[]{seedStr, NLFSRCommon.getKey(seedStr), function.getResult(nlfsr.getSeedArray(seedStr)), newSeedOk});
        model.addRow(new Object[]{"","","",""});
        
        nlfsr.Execute();
    }
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        //validate step 5        
        
        try{
            String key = jTextField3.getText();
            int period = Integer.parseInt(jTextField4.getText());
            int number = Integer.parseInt(jTextField5.getText());

            String keyOk = nlfsr.getKey();
            if(key.equals(keyOk) && period == keyOk.length() && number == Integer.parseInt(keyOk, 2))
                showPopup(jPanel5, "Ejercicio completado correctamente.");
            else
                showPopup(jPanel5, "Respuesta incorrecta. Intentelo de nuevo.");

        }catch(Exception e){
            showPopup(jPanel5, "Respuesta incorrecta. Intentelo de nuevo.");
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setValueAt(String.valueOf(NLFSRCommon.getKey(seedStr)), 0, 1);
        model.setValueAt(function.getResult(nlfsr.getSeedArray(seedStr)), 0, 2);
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
        model.setValueAt(NLFSRCommon.getSeedString(nlfsr.shiftSeed(nlfsr.getSeedArray(seedStr), s0)), 0, 3);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        
        String key = nlfsr.getKey();
        jTextField3.setText(key);
        jTextField4.setText(Integer.toString(key.length()));
        jTextField5.setText(String.valueOf(Integer.parseInt(key,2)));
    }//GEN-LAST:event_jButton12ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton6ActionPerformed

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
        showRightAnswers();
        setWrongAnswers(wronganswers);
                
    }//GEN-LAST:event_jButton13ActionPerformed

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
        buttonGroup1.clearSelection();
        buttonGroup2.clearSelection();
        buttonGroup3.clearSelection();
        buttonGroup4.clearSelection();
        resetForeground(wronganswers);
        resetForeground(rightanswers);
        showRightAnswers();
    }//GEN-LAST:event_jButton14ActionPerformed

    private void resetForeground(ArrayList<JRadioButton> buttons){
        for (JRadioButton button : buttons) {
            button.setForeground(Color.BLACK);
        }
    }
    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
        //show step 5
        jTabbedPane1.setEnabledAt(1, true);
        jTabbedPane1.setSelectedIndex(1);
    }//GEN-LAST:event_jButton15ActionPerformed

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton16ActionPerformed
        //Validar iteracion
        try{
            it = jTable3.getRowCount() - 1;
            DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
            String r = model.getValueAt(it, 0).toString();
            String k = model.getValueAt(it, 1).toString();
            String x = model.getValueAt(it, 2).toString();
            String n = model.getValueAt(it, 3).toString();


            String rOk = model.getValueAt(it-1, 3).toString();
            int rOkArray[] = nlfsr.getSeedArray(rOk);
            String kOk = String.valueOf(NLFSRCommon.getKey(rOk));
            String xOk = String.valueOf(function.getResult(rOkArray));
            String nOk = NLFSRCommon.getSeedString(nlfsr.shiftSeed(rOkArray, Integer.parseInt(xOk)));

            if(r.equals(rOk) && k.equals(kOk) && x.equals(xOk) && n.equals(nOk)){
                showPopup(jPanel5, "Los valores ingresados son correctos.");
                if(it == maxIt){
                    jButton16.setEnabled(false);
                    jButton17.setEnabled(false);
                    jTextField3.setEnabled(true);
                    jTextField4.setEnabled(true);
                    jTextField5.setEnabled(true);
                    jButton12.setEnabled(true);
                    jButton10.setEnabled(true);
                }else{
                    model.addRow(new Object[]{"","","",""});
                }                
            }            
            else{
                showPopup(jPanel5, "Los valores ingresados son incorrectos.");

            }
        }catch(Exception e){
            showPopup(jPanel5, "Los valores ingresados son incorrectos.");
        }
        
            
    }//GEN-LAST:event_jButton16ActionPerformed

    private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton17ActionPerformed
        // Calculate iteration
        it = jTable3.getRowCount() - 1;
        DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
        String rOk = model.getValueAt(it-1, 3).toString();
        int rOkArray[] = nlfsr.getSeedArray(rOk);
        String kOk = String.valueOf(NLFSRCommon.getKey(rOk));
        String xOk = String.valueOf(function.getResult(rOkArray));
        String nOk = NLFSRCommon.getSeedString(nlfsr.shiftSeed(rOkArray, Integer.parseInt(xOk)));
        model.setValueAt(rOk, it, 0);
        model.setValueAt(kOk, it, 1);
        model.setValueAt(xOk, it, 2);
        model.setValueAt(nOk, it, 3);
        if(it == maxIt){
            jButton16.setEnabled(false);
            jButton17.setEnabled(false);
            jTextField3.setEnabled(true);
            jTextField4.setEnabled(true);
            jTextField5.setEnabled(true);
            jButton12.setEnabled(true);
            jButton10.setEnabled(true);
        }else{
            model.addRow(new Object[]{"","","",""});
        }
    }//GEN-LAST:event_jButton17ActionPerformed

    private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton18ActionPerformed
        //Paso 1 - guardar
        saveFile(1);
    }//GEN-LAST:event_jButton18ActionPerformed

    private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton19ActionPerformed
        // Paso 2 - Guardar
        saveFile(2);
    }//GEN-LAST:event_jButton19ActionPerformed

    private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton20ActionPerformed
        // Paso 3 - Guardar
        saveFile(3);
    }//GEN-LAST:event_jButton20ActionPerformed

    private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed
        // Paso 4 - Guardar
        saveFile(4);
    }//GEN-LAST:event_jButton21ActionPerformed

    private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed
        // Paso 5 - Guardar
        saveFile(5);
    }//GEN-LAST:event_jButton22ActionPerformed

    private NLFSRDTO messageToJson(int step){
        
        NLFSRDTO dto = new NLFSRDTO();
        dto.setCurrentStep(step);
        dto.setSeed(this.jTextField1.getText());
        dto.setRegister(this.jTextField2.getText());
        if(step > 1)
            dto.setFunctionId(this.jComboBox1.getSelectedIndex());
        if(step > 2){
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            String[] list = new String[3];
            for (int i = 0; i < model.getColumnCount(); i++) {
                list[i] = model.getValueAt(0, i).toString();
            }
            dto.setRowStep3(list);
        }
        if(step > 3){
            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
            String[] list = new String[4];
            for (int i = 0; i < model.getColumnCount(); i++) {
                list[i] = model.getValueAt(0, i).toString();
            }
            dto.setRowStep4(list);
        }
        if(step > 4){
            DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
            String[][] table = new String[16][4];
            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < model.getColumnCount(); j++) {
                    table[i][j] = model.getValueAt(i, j).toString();
                }
            }
            dto.setTableStep5(table);
            dto.setKey(this.jTextField3.getText());
            dto.setPeriod(this.jTextField4.getText());
            dto.setKeyDecimal(this.jTextField5.getText());
        }
        return dto;
    }
    
    private void saveFile(int step){
        try {
            JFileChooser saveFile = new JFileChooser();
            saveFile.showSaveDialog(null);
            String path=saveFile.getSelectedFile().getAbsolutePath();
            String filename=saveFile.getSelectedFile().getName();
            AlgorithmStateStorage.saveNLFSR(filename, path, this.messageToJson(step));
            JOptionPane.showMessageDialog(null, "El archivo se salvo correctamente");
        } catch (IOException ex) {
            Logger.getLogger(NLFSREvaluar.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Hubo un error al guardar el archivo");
        } catch (Exception e) {
            Logger.getLogger(NLFSREvaluar.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    
    public void load(NLFSRDTO nlfsrDTO) {
        int step = nlfsrDTO.getCurrentStep();
        
        this.jTextField1.setText(nlfsrDTO.getSeed());
        this.jTextField2.setText(nlfsrDTO.getRegister());
        if(step > 1){
            //update step 2
            enableStep1();
            setUpStep2();
            this.jComboBox1.setSelectedIndex(nlfsrDTO.getFunctionId());
        }
        if(step > 2){
            //update step 3
            enableStep2();
            setUpStep3();
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            String[] list = nlfsrDTO.getRowStep3();
            for (int i = 0; i < model.getColumnCount(); i++) {
                model.setValueAt(list[i], 0, i);
            }
        }
        if(step > 3){
            //update step 4
            enableStep3();
            setUpStep4();
            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
            String[] list = nlfsrDTO.getRowStep4();
            for (int i = 0; i < model.getColumnCount(); i++) {
                model.setValueAt(list[i], 0, i);
            }
        }
        if(step > 4){
            //update step 5
            enableStep4();
            setUpStep5();
            DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
            String[][] table = nlfsrDTO.getTableStep5();
            for (int i = 0; i < 16; i++) {
                for (int j = 0; j < 4; j++) {
                    model.setValueAt(table[i][j], i, j);
                    model.addRow(new Object[]{"","","",""});
                }
            }
            this.jTextField3.setText(nlfsrDTO.getKey());
            this.jTextField4.setText(nlfsrDTO.getPeriod());
            this.jTextField5.setText(nlfsrDTO.getKeyDecimal());
            
            if(nlfsrDTO.getKey() != null && !nlfsrDTO.getKey().isEmpty() 
                    || nlfsrDTO.getPeriod() != null && !nlfsrDTO.getPeriod().isEmpty()
                    || nlfsrDTO.getKeyDecimal() != null && !nlfsrDTO.getKeyDecimal().isEmpty()){
                this.jTextField3.setEnabled(true);
                this.jTextField4.setEnabled(true);
                this.jTextField5.setEnabled(true);
            }                
        }
        
        for (int i = 1; i <= step; i++) {
            jTabbedPane1.setEnabledAt(i, true);
        }
        jTabbedPane1.setSelectedIndex(step);
    }
    
    private void setWrongAnswers(ArrayList<JRadioButton> buttons){
        for (JRadioButton button : buttons) {
            if (button.isSelected()) {
                button.setForeground(Color.red);
            }        
        }
    }
    
    private void showRightAnswers(){
        
        Color green = new Color(16, 182, 21);
        
        jRadioButton1.setForeground(green);
        jRadioButton6.setForeground(green);
        jRadioButton9.setForeground(green);
        jRadioButton10.setForeground(green);
    }
    private void showPopup(JPanel panel, String message){
        JOptionPane.showMessageDialog(panel, message, "Validacion", WIDTH);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton18;
    private javax.swing.JButton jButton19;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton20;
    private javax.swing.JButton jButton21;
    private javax.swing.JButton jButton22;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton10;
    private javax.swing.JRadioButton jRadioButton11;
    private javax.swing.JRadioButton jRadioButton12;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JRadioButton jRadioButton9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
